﻿using CleanArchitecture.Core.Handlers;
using CleanArchitecture.Core.Interfaces;
using CleanArchitecture.Infrastructure.Entities.Business;
using CleanArchitecture.Infrastructure.Entities.Customers;
using CleanArchitecture.Infrastructure.Interfaces.Business;
using System;

namespace CleanArchitecture.Infrastructure.ActiveLogExtension
{
    public static class ActiveLogExtension
    {
        /// <summary>
        /// Log Customer LoginActive
        /// </summary>
        /// <param name="entity">Entity</param>
        /// <returns>Result</returns>
        public static void LogCustomerLoginEvent(this Customer customer, IWebHelper webHelper = null, IActiveLogService logService = null)
        {
            if (customer == null)
                throw new ArgumentNullException(nameof(customer));
            if(webHelper == null)
                webHelper = EngineContext.Current.Resolve<IWebHelper>();
            if(logService == null)
                logService = EngineContext.Current.Resolve<IActiveLogService>();
            var time = DateTime.UtcNow;
            var logitem = new ActiveLog
            {
                ActiveName = ActiveLogName.CustomerLogin,
                EntityId = customer.Id,
                EntityName = customer.GetType().Name,
                CustomerId = customer.Id,
                Detail = $"前端注册用户：Id: {customer.Id}, 登录账号: {customer.Mobile}, 成功登录系统。",
                IpAddress = webHelper.GetCurrentIpAddress(),
                CreatedOnTimeUtc = time,
                Month = time.Month,
                Year = time.Year
            };
            logService.InsertLog(logitem);
        }

        public static void LogVisitNewsEvent(this NewsItem news, IWebHelper webHelper = null, IActiveLogService logService = null)
        {
            if (news == null)
                throw new ArgumentNullException(nameof(news));
            if (webHelper == null)
                webHelper = EngineContext.Current.Resolve<IWebHelper>();
            if (logService == null)
                logService = EngineContext.Current.Resolve<IActiveLogService>();
            var time = DateTime.UtcNow;
            var logitem = new ActiveLog
            {
                ActiveName = ActiveLogName.VisitNewsPage,
                EntityId = news.Id,
                EntityName = news.GetType().Name,
                CustomerId = 0,
                Detail = "用户浏览新闻/文章",
                IpAddress = webHelper.GetCurrentIpAddress(),
                CreatedOnTimeUtc = time,
                Month = time.Month,
                Year = time.Year
            };
            logService.InsertLog(logitem);
        }

        public static void LogSearchNewsEvent(this NewsItem news, string keyword, IWebHelper webHelper = null, IActiveLogService logService = null)
        {
            if (news == null)
                throw new ArgumentNullException(nameof(news));
            if (webHelper == null)
                webHelper = EngineContext.Current.Resolve<IWebHelper>();
            if (logService == null)
                logService = EngineContext.Current.Resolve<IActiveLogService>();
            var time = DateTime.UtcNow;
            var logitem = new ActiveLog
            {
                ActiveName = ActiveLogName.SearchNewsKeyWord,
                EntityId = news.Id,
                EntityName = news.GetType().Name,
                CustomerId = 0,
                Detail = keyword,
                IpAddress = webHelper.GetCurrentIpAddress(),
                CreatedOnTimeUtc = time,
                Month = time.Month,
                Year = time.Year
            };
            logService.InsertLog(logitem);
        }

        public static void LogSearchBlogEvent(this BlogItem blog, string keyword, IWebHelper webHelper = null, IActiveLogService logService = null)
        {
            if (blog == null)
                throw new ArgumentNullException(nameof(blog));
            if (webHelper == null)
                webHelper = EngineContext.Current.Resolve<IWebHelper>();
            if (logService == null)
                logService = EngineContext.Current.Resolve<IActiveLogService>();
            var time = DateTime.UtcNow;
            var logitem = new ActiveLog
            {
                ActiveName = ActiveLogName.SearchBlogKeyWord,
                EntityId = blog.Id,
                EntityName = blog.GetType().Name,
                CustomerId = 0,
                Detail = keyword,
                IpAddress = webHelper.GetCurrentIpAddress(),
                CreatedOnTimeUtc = time,
                Month = time.Month,
                Year = time.Year
            };
            logService.InsertLog(logitem);
        }

        public static void LogVisitBlogEvent(this BlogItem blog, IWebHelper webHelper = null, IActiveLogService logService = null)
        {
            if (blog == null)
                throw new ArgumentNullException(nameof(blog));
            if (webHelper == null)
                webHelper = EngineContext.Current.Resolve<IWebHelper>();
            if (logService == null)
                logService = EngineContext.Current.Resolve<IActiveLogService>();
            var time = DateTime.UtcNow;
            var logitem = new ActiveLog
            {
                ActiveName = ActiveLogName.VisitBlogPage,
                EntityId = blog.Id,
                EntityName = blog.GetType().Name,
                CustomerId = 0,
                Detail = "匿名用户浏览博客文章",
                IpAddress = webHelper.GetCurrentIpAddress(),
                CreatedOnTimeUtc = time,
                Month = time.Month,
                Year = time.Year
            };
            logService.InsertLog(logitem);
        }

        public static void LogLikeBlogEvent(this BlogItem blog, IWebHelper webHelper = null, IActiveLogService logService = null)
        {
            if (blog == null)
                throw new ArgumentNullException(nameof(blog));
            if (webHelper == null)
                webHelper = EngineContext.Current.Resolve<IWebHelper>();
            if (logService == null)
                logService = EngineContext.Current.Resolve<IActiveLogService>();
            var time = DateTime.UtcNow;
            var logitem = new ActiveLog
            {
                ActiveName = ActiveLogName.BlogPageLike,
                EntityId = blog.Id,
                EntityName = blog.GetType().Name,
                CustomerId = 0,
                Detail = "匿名用户点赞博客文章",
                IpAddress = webHelper.GetCurrentIpAddress(),
                CreatedOnTimeUtc = time,
                Month = time.Month,
                Year = time.Year
            };
            logService.InsertLog(logitem);
        }
    }
}
